package com.sunmouse.toollib.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

import java.util.List;

/**
 * Created by Kvin on 2017/8/9.
 */
public abstract class BasicExpandableAdapter<D extends Child, G extends BasicExpandableAdapter.ViewHolder, C extends BasicExpandableAdapter.ViewHolder> extends BaseExpandableListAdapter {
    protected List<D> list;
    protected LayoutInflater inflater;
    protected Context context;

    public BasicExpandableAdapter(List<D> list, Context context) {
        this.list = list;
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getGroupCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return list.get(groupPosition) == null ? 0 : list.get(groupPosition).getChildList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list == null ? null : list.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition) == null ? null : list.get(groupPosition).getChildList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        G g;
        if (convertView == null) {
            convertView = bindGroupView(inflater, parent);
            g = bindGroupHolder(convertView);
            convertView.setTag(g);
        } else {
            g = (G) convertView.getTag();
        }
        bindGroupData(g,list,groupPosition,isExpanded);
        return convertView;
    }

    protected abstract View bindGroupView(LayoutInflater inflater, ViewGroup parent);

    protected abstract G bindGroupHolder(View cv);

    protected abstract void bindGroupData(G g, List<D> list, int groupPosition, boolean isExpanded);

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        C c;
        if (convertView == null) {
            convertView = bindChildView(inflater,parent);
            c = bindChildHolder(convertView);
            convertView.setTag(c);
        } else {
            c = (C) convertView.getTag();
        }
        bindChildData(c,list,groupPosition,childPosition);
        return convertView;
    }

    protected abstract View bindChildView(LayoutInflater inflater, ViewGroup parent);

    protected abstract C bindChildHolder(View cv);

    protected abstract void bindChildData(C c, List<D> list, int groupPosition, int childPosition);

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public static class ViewHolder {

        public ViewHolder(View v) {
        }
    }

}